package com.dynamic.mybatis.core.session.helper;

import com.dynamic.mybatis.core.intercept.DynamicMappedStatementHandler;
import com.dynamic.mybatis.core.metadata.DynamicConfiguration;
import com.dynamic.mybatis.core.metadata.DynamicMappedStatement;
import com.dynamic.mybatis.core.parser.field.DynamicMixedMappedStatementFieldParser;
import com.dynamic.mybatis.core.parser.xml.DynamicMappedStatementParser;
import com.dynamic.mybatis.core.session.DynamicSqlSession;
import org.apache.ibatis.mapping.MappedStatement;

import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * @Description
 * @Author xs
 * @Date 2023/3/15 9:36
 */
public class DynamicMappedStatementHelper {

    protected DynamicMixedMappedStatementFieldParser mixedMappedStatementFieldParser;
    protected DynamicMappedStatementParser mappedStatementParser;
    protected DynamicSqlSession dynamicSqlSession;

    public DynamicMappedStatementHelper(DynamicSqlSession dynamicSqlSession) {
        this.dynamicSqlSession = dynamicSqlSession;
        this.mixedMappedStatementFieldParser = new DynamicMixedMappedStatementFieldParser(dynamicSqlSession);
        this.mappedStatementParser = new DynamicMappedStatementParser(dynamicSqlSession);
    }

    public DynamicMixedMappedStatementFieldParser getMixedMappedStatementFieldParser() {
        return mixedMappedStatementFieldParser;
    }

    public DynamicMappedStatementParser getMappedStatementParser() {
        return mappedStatementParser;
    }

    public MappedStatement getMappedStatement(String mappedStatementId) {
        return this.dynamicSqlSession.getSqlSession().getConfiguration().getMappedStatement(mappedStatementId);
    }

    public DynamicMappedStatement getDynamicMappedStatement(String mappedStatementId) {
        return ((DynamicConfiguration) this.dynamicSqlSession.getSqlSession().getConfiguration()).getDynamicMappedStatement(mappedStatementId);
    }

    public void addDynamicMappedStatement(List<DynamicMappedStatement> dmsList) {
        for (DynamicMappedStatement dms : dmsList) {
            this.addDynamicMappedStatement(dms, false);
        }
    }

    public void addDynamicMappedStatement(List<DynamicMappedStatement> dmsList, boolean handler) {
        for (DynamicMappedStatement dms : dmsList) {
            this.addDynamicMappedStatement(dms, handler);
        }
    }

    public DynamicMappedStatement addDynamicMappedStatement(DynamicMappedStatement dms, boolean handler) {
        DynamicConfiguration dynamicConfiguration = (DynamicConfiguration) dynamicSqlSession.getSqlSession().getConfiguration();
        dynamicConfiguration.addDynamicMappedStatement(dms, handler);
        return dms;
    }

    public void addParseDynamicMappedStatement(List<DynamicMappedStatement> dmsList) {
        for (DynamicMappedStatement dms : dmsList) {
            this.addParseDynamicMappedStatement(dms, false);
        }
    }

    public void addParseDynamicMappedStatement(List<DynamicMappedStatement> dmsList, boolean handler) {
        for (DynamicMappedStatement dms : dmsList) {
            this.addParseDynamicMappedStatement(dms, handler);
        }
    }


    public DynamicMappedStatement addParseDynamicMappedStatement(DynamicMappedStatement dms, boolean handler) {
        mappedStatementParser.parseDynamicMappedStatement(dms, handler);
        return dms;
    }

    public Collection<DynamicMappedStatement> mappedStatementValues() {
        return ((DynamicConfiguration) dynamicSqlSession.getSqlSession().getConfiguration()).values();
    }


    public Set<DynamicMappedStatementHandler> mappedStatementHandlers() {
        return ((DynamicConfiguration) dynamicSqlSession.getSqlSession().getConfiguration()).mappedStatementHandlers();
    }
}